/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package grest.controller;

import grest.model.DataIO;
import grest.view.VistaEstadisticas;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Agustin
 */
public class CntrlrTabsEstadisticas implements ChangeListener{
    VistaEstadisticas form;
    
    
    public CntrlrTabsEstadisticas(VistaEstadisticas form){
        this.form = form;
    }
    
    @Override
    public void stateChanged(ChangeEvent e) {
        JTabbedPane sourceTabbedPane = (JTabbedPane) e.getSource();
        int index = sourceTabbedPane.getSelectedIndex();
        try {
            changePanel(index);
        } catch (SQLException ex) {
          //  Logger.getLogger(CntrlrEstadisticas.class.getName()).log(Level.SEVERE, null, ex);
            System.out.print(ex.getMessage());
        }
    }
    
    public void changePanel(Integer index) throws SQLException{
        switch(index){
      /*  case 0:  setMontoTotal()   ;

        break;*/
        case 1: setPanelHistReservas() ;

        break;
        case 2: setProductosPedidos() ;

        break;
        case 3: setPanelReservasClientes();
        break;
        }
    }
    
    
    public void setPanelReservasClientes() throws SQLException{
        JTable tablaReservas = (JTable)form.getControls("tblReservasClientes");
        String[] headers= {"NRO_CLIENTE"," #APELLIDO", "NOMBRE", "TOTAL_RESERVAS","CONCRETADAS","CANCELADAS"};
        DefaultTableModel modelReservas = new DefaultTableModel(headers,0);
        tablaReservas.setModel(modelReservas);
        String query = "Select nro_cliente,apellido,nombre,total_reservas,concretadas,canceladas from v_reservas";
        ResultSet rs = DataIO.getResultSet(query);
        while (rs.next()){
            String[] fila = new String[6];
            fila[0]=String.valueOf(rs.getInt("nro_cliente"));
            fila[1]=rs.getString("apellido");
            fila[2]=rs.getString("nombre");
            fila[3]=String.valueOf(rs.getInt("total_reservas"));
            fila[4]=String.valueOf(rs.getInt("concretadas"));
            fila[5]=String.valueOf(rs.getInt("canceladas"));
            modelReservas.addRow(fila);
        }
    }
    
    public void setPanelHistReservas() throws SQLException{
        String totales = null,concretadas = null,canceladas = null;
        String query = "Select totales,concretadas,canceladas from v_estad_reserv";
        ResultSet rs = DataIO.getResultSet(query);
        while (rs.next()){
            if (rs.getInt("totales")==0)
                totales="0";
            else totales = String.valueOf(rs.getInt("totales"));
            if (rs.getInt("concretadas")==0)
                concretadas="0";
            else concretadas = String.valueOf(rs.getInt("concretadas"));
            if (rs.getInt("canceladas")==0)
                canceladas="0";
            else 
                canceladas = String.valueOf(rs.getInt("canceladas"));
           ////////////////////////////////////////////////////////////
            ((JLabel)form.getControls("lblValorTotales")).setText(totales);
           ((JLabel)form.getControls("lblValorCanceladas")).setText(canceladas);
           ((JLabel)form.getControls("lblValorConcretadas")).setText(concretadas);
        }

    }
    
    public void setProductosPedidos() throws SQLException{
        JTable tablaPedidos = (JTable)form.getControls("tblPedidoProductos");
        String[] headers= {"NRO_CLIENTE","NOMBRE_CLIENTE","ID_PRODUCTO","DESCRIPCION", "CANTIDAD_PEDIDOS"};
        DefaultTableModel modelReservas = new DefaultTableModel(headers,0);
        tablaPedidos.setModel(modelReservas);
        String query = "Select nro_cliente,nombre_cliente,id_producto,descripcion,cantidad_pedidos from v_prod_ped_detalle";
        ResultSet rs = DataIO.getResultSet(query);
        while (rs.next()){
            String[] fila = new String[5];
            fila[0]= String.valueOf(rs.getInt("nro_cliente"));
            fila[1]= rs.getString("nombre_cliente");
            fila[2]= String.valueOf(rs.getInt("id_producto"));
            fila[3]= rs.getString("descripcion");
            fila[4]= String.valueOf(rs.getInt("cantidad_pedidos"));
            modelReservas.addRow(fila);
        }
        
    }
}